package hu.monitor.remoting.transport;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServerEndpointAdapter {
	private static Logger log = LoggerFactory.getLogger(ServerEndpointAdapter.class);

	private final Marshaller marshaller;
	private final RequestProcessor processor;

	public ServerEndpointAdapter(final Marshaller marshaller, final RequestProcessor processor) {
		this.marshaller = marshaller;
		this.processor = processor;
	}

	public void processRequest(final InputStream input, final OutputStream output) throws ClassNotFoundException, IOException {
		final Object object = this.marshaller.unmarshall(input);
		Object response = null;
		try {
			response = this.processor.processRequest(object);
		} catch (final Throwable ex) {
			log.error(ex.getMessage(), ex);
			response = ex;
		}

		this.marshaller.marshall(response, output);
	}
}
